package com.example.config;

import com.example.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * @author itheima
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    // 对swagger的请求不进行拦截
    String[] excludePatterns = new String[]{"/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**",
            "/api", "/api-docs", "/api-docs/**", "/doc.html/**"};
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // addInterceptor 添加什么拦截器
        // addPathPatterns 过滤哪些路径
        // /** 从/开始的后面无限层级的路径 都会匹配
        // /user/info/test
        // /* 从/开始后面加上任意字符
        // /user, /orders, /test
        // /user/info 不匹配
        // excludePathPatterns 放行哪些路径
        registry.addInterceptor(new TokenInterceptor(redisTemplate))
                .addPathPatterns("/**")
                .excludePathPatterns(excludePatterns);
//                .excludePathPatterns("/users/sendMsg", "/users/login","/doc.html","/webjars/**");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
